
var https = "https://www.moreclub.cn/";
var WxParse = require('../../wxParse/wxParse.js');

var util = require('../../utils/util.js')
var app = getApp();
var page = 0;
var city;

var getList = function list(that,city) {
  wx.request({
    url: https + 'v1/merchant/open/mchantlist/'+city,
    data: {
      pn: page,
      ps: 10
    },
    success: function (res) {
      // success
      var listdata = res.data;
      if (listdata.success) {
        var list = listdata.data;
        var curlist = [];//创建一个新数组
        for (var key in list) {
          var oldobj = list[key].tags;
          var tags = oldobj.split(",");//标签
          if (tags != null && tags.length > 3) {
            tags = tags.slice(0, 3);
          }
          var name = list[key].name;//酒吧名
          var mid = list[key].mid;//商家id
          var obj = {};//创建新对象
          obj.tags = tags;
          obj.name = name;
          obj.mid = mid;
          curlist.push(obj);
        };

        if (curlist != null) {
          //that.data.totallist.concat(curlist);
          //var oldlist = that.data.totallist;

          var totallist = that.data.options
          console.log(totallist)
          // for (var i = 0; i < curlist.length; i++) {
          //   oldlist.push(curlist[i])
          // }
        }
        that.setData({
          curlist: curlist,
          totallist: totallist
        })
      }
    },
  })
}

// var merchantoption;
Page({
  data: {
    indicatorDots: false,
    autoplay: true,
    circular: true,
    interval: 3000,
    duration: 500,
    toView: 'red',
    scrollLeft: 0,
    show: false,
    selectindex: 0,
    listpn: 0,
    totallist: []
  },
  changeIndicatorDots: function (e) {
    this.setData({
      indicatorDots: !this.data.indicatorDots
    })
  },
  changeAutoplay: function (e) {
    this.setData({
      autoplay: !this.data.autoplay
    })
  },
  intervalChange: function (e) {
    this.setData({
      interval: e.detail.value
    })
  },
  durationChange: function (e) {
    this.setData({
      duration: e.detail.value
    })
  },
  swiperChange: function (event) {
    //console.log(this.data)
    var dots = this.data.dotsclass;
    for (var i = 0; i < dots.length; i++) {
      dots[i] = "";
    }
    dots[event.detail.current] = 'on';
    this.setData({
      dotsclass: dots
    });
  },
 
  tap: function (e) {
    for (var i = 0; i < order.length; ++i) {
      if (order[i] === this.data.toView) {
        this.setData({
          toView: order[i + 1]
        })
        break
      }
    }
  },
  tapMove: function (e) {
    this.setData({
      scrollTop: this.data.scrollTop + 10
    })
  },
  openMap: function (event) {
    console.log(event)
    wx.openLocation({
      latitude: event.target.dataset.lat, // 纬度，范围为-90~90，负数表示南纬
      longitude: event.target.dataset.lng, // 经度，范围为-180~180，负数表示西经
      scale: 28, // 缩放比例          
    })
  },
  call:function(e){
   
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.num, 
    })  
  },
  showview: function (event) {
    var that = this;
    that.setData({
      show: true,
      selectindex: event.target.dataset.index
    })

  },
  hideview: function () {
    var that = this;
    that.setData({
      show: false
    })

  },
  onShow:function(){
    var that = this;
   
  },

  onLoad: function (options) {
    var that = this;
    city = wx.getStorageSync('city')
    var uid = wx.getStorageSync('uid');
    // merchantoption = options;
    that.setData({
      options: options
    });
    var title = options.title;
    var mid = options.mid;
    try {
      wx.setStorageSync('mid', mid);
      wx.setStorageSync('title', title);
    } catch (e) {
    }
    wx.setNavigationBarTitle({//设置标题
      title: options.title
    });
    page = 0;
    getList(that,city);

    console.log(title);

   

    wx.request({//商家详情请求
      url: https + 'v1/merchant/open/detail/'+mid + '?uid=' + uid,
      dataType: "json",
      success: function (res) {
        // success
        var respData = res.data;
        if (respData.success) {
          var business = respData.data
          console.log(business)
          var pictures = business.pictures;//相册
          var dots = [];//轮播指示器
          for (var i = 0; i < pictures.length; i++) {
            dots.push("");
          }
          dots[0] = 'on';
          var location = {};
          //location.lat = business.lat;
          //location.lng = business.lng;

          var temp = util.bdDecrypt(business.lng, business.lat);
          location.lat = temp.lat;
          location.lng = temp.lng;
          var images = business.scenePictureList
          for (var i = 0; i < images.length; i++) {
            var item = images[i];
            item.index = i;
          }

          that.setData({
            business: business,
            dotsclass: dots,
            location: location
          })
          //console.log(business.promoList)
        }
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })



    wx.request({//富文本框请求
      url: https + 'v1/merchant/open/richtext/'+mid,
      dataType: "json",
      success: function (res) {
        // success
        var respData = res.data;
        if (respData.success) {
          var richtext = respData.data;
          WxParse.wxParse('richtext', 'html', richtext, that, 5);
        }
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
  },
  
  onShareAppMessage: function () {
    // 用户点击右上角分享
    var mid = wx.getStorageSync('mid');
    var title = wx.getStorageSync('title');
    return {
      title: title, // 分享标题
      desc: title, // 分享描述
      path: '/pages/rich/rich?title=' + title+"&mid=" + mid  // 分享路径
    }
  },

  

})


